<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <link rel="stylesheet" href="./libs/layui/css/layui.css">
    <script src="./libs/jquery/jquery.min.js"></script>
    <script src="./libs/layui/layui.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <div class="form-inline">
                        <input type="text" id="aname" class="form-control input-sm" placeholder="输入文章标题...">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <option value="">所有分类</option>
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </div>
                </div>
                <div class="col-xs-6">
                    <a href="./article_add.html" class="btn btn-success btn-sm pull-right" id="release_btn" >发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody id="code">
                </tbody>
            </table>
            <div class="row text-center" id="test1">
            </div>
        </div>
    </div>
    <script src="./js/http.js"></script>
    <script src="./libs/art-template/template-web.js"></script>
    <!-- 所有分类 -->
    <script type="text/html" id="ListClass">
            {{each data }}
            <option value={{ $value.id }}>{{ $value.name }}</option>
            {{/each}}
    </script>
    <!-- 数据列表 -->
    <script type="text/html" id="listCode">
        {{each data}}
        <tr>
            <td>{{$value.title}}</td>
            <td>{{$value.author}}</td>
            <td>{{$value.category}}</td>
            <td class="text-center">{{$value.date}}</td>
            <td class="text-center">{{$value.state}}</td>
            <td class="text-center">
                <a href="article_edit.html?id={{$value.id}}" class="btn btn-default btn-xs">编辑</a>
                <a href="javascript:void(0);" class="btn btn-danger btn-xs delete" onclick="del({{$value.id}})">删除</a>
            </td>
        </tr>
        {{/each}}
    </script>
    <script>
        $(function () {
            //把分类放到所有分类中中
            listClass()
            //打开页面渲染数据列表
            listCode()
        })
        //把分类放到所有分类中中
        function listClass() {
            myHeader('/admin/category/list', 'get', data = null, function (resData) {
                $('#selCategory').append(template('ListClass', resData));
            })
        }

        // 点击的时候调用渲染数据列表,传入参数筛选
        $('#btnSearch').click(function () {
            listCode();
        })

        //渲染数据列表
        function listCode(page = 1, perpage = 6) {
            let option = {
                key: $('#aname').val(),
                type: $('#selCategory').val(),
                state: $('#selStatus').val(),
                page: page,
                perpage: perpage,
            }

            myHeader('/admin/article/query', 'get', data = option, function (resData) {
                $('#code').html(template('listCode', resData.data))
                //分页器
                layui.use('laypage', function () {
                    let laypage = layui.laypage;
                    //执行一个laypage实例
                    laypage.render({
                        elem: 'test1', //注意，这里的 test1 是 ID，不用加 # 号
                        count: resData.data.totalCount, //数据总数，从服务端得到
                        limit: 6,
                        curr: option.page,
                        jump: function (obj, first) {
                            //obj包含了当前分页的所有参数，比如：
                            // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                            // console.log(obj.limit); //得到每页显示的条数

                            //首次不执行
                            if (!first) {
                                listCode(obj.curr, obj.limit);
                            }
                        }
                    });
                });
            })
        }

        //删除,有bug
        function del(id_d) {
            console.log(id_d);
            myHeader('/admin/article/delete', 'post', { id: id_d }, function (resData) {
                listCode()
            })
        }

    </script>
</body>

</html>